package com.buddies.lottery.ui.lottery.detail;

import com.buddies.lottery.base.BasePresenter;
import com.buddies.lottery.common.ConfigManager;
import com.buddies.lottery.data.BaseObserver;
import com.buddies.lottery.data.DataManager;
import com.buddies.lottery.data.bean.BetItem;
import com.buddies.lottery.data.resp.BalanceResponse;
import com.buddies.lottery.data.resp.LotteryDetailResponse;
import com.buddies.lottery.data.resp.LotteryTabOptionsResponse;
import com.buddies.lottery.data.resp.PostBetResponse;

import java.util.List;

/**
 * Created by xuliang on 2018/2/28.
 */

public class LotteryDetailPresenter extends BasePresenter<LotteryDetailContract.View> implements LotteryDetailContract.Presenter {

    public LotteryDetailPresenter(LotteryDetailContract.View view) {
        super(view);
    }

    @Override
    public void loadLotteryDetail(int lotteryId) {
        addSubscription(DataManager.getInstance().getLotteryRepo().getLotteryDetail(lotteryId), new BaseObserver<LotteryDetailResponse>() {
            @Override
            public void onNext(LotteryDetailResponse response) {
                mView.onLotteryDetailLoaded(response);
            }

            @Override
            public void onError(Throwable e) {
               mView.onError(e);
            }
        });
    }

    @Override
    public void loadLotteryTabOptions(String lotteryTabQueryId) {
        addSubscription(DataManager.getInstance().getLotteryRepo().getLotteryTabOptions(lotteryTabQueryId), new BaseObserver<LotteryTabOptionsResponse>() {
            @Override
            public void onNext(LotteryTabOptionsResponse response) {
                mView.onLotteryTabOptionsLoaded(response);
            }

            @Override
            public void onError(Throwable e) {
                mView.onError(e);
            }
        });
    }

    @Override
    public void loadBalance() {
        addSubscription(DataManager.getInstance().getUserRepo().getBalance(), new BaseObserver<BalanceResponse>() {
            @Override
            public void onNext(BalanceResponse response) {
                ConfigManager.getInstance().setBalance(response.balance);
                mView.onUpdateBalance();
            }

            @Override
            public void onError(Throwable e) {
                mView.onError(e);
            }
        });
    }

    @Override
    public void postBet(int lotteryId, String period, List<BetItem> betItems) {
        addSubscription(DataManager.getInstance().getLotteryRepo().postBet(lotteryId, period, betItems), new BaseObserver<PostBetResponse>() {
            @Override
            public void onNext(PostBetResponse response) {
                ConfigManager.getInstance().setBalance(response.balance);
                mView.onPostBetSuccess(response);
            }

            @Override
            public void onError(Throwable e) {
                mView.onError(e);
            }
        });
    }

}
